<template>
  <!-- 发运方管理 -->
  <div class="shareContainer" ref="shareContainer">
    <!-- 头部搜索功能 -->
    <!-- <div class="shareheader">
      <div class="shareheader_top">
        <div style="min-width:260px;margin: 5px 10px 18px 0px;">
          <span style="font-size: 13px; padding: 0px 8px 0px 0px">收货人</span>
          <el-input v-model="inputname" placeholder="请输入收货人名称" style="width: 210px"></el-input>
        </div>
        <div style="min-width:260px;margin: 5px 10px 18px 0px;">
          <span style="font-size: 13px; padding: 0px 8px 0px 0px">联系方式</span>
          <el-input v-model="inputPhone" placeholder="请输入收货人联系方式" style="width: 210px"></el-input>
        </div>
        <template v-if="!isCustomer">
        <div style="min-width:200px;margin: 5px 10px 18px 0px;">
          <span style="font-size: 13px; padding: 0px 8px 0px 0px">所属组织</span>
          <el-select v-model="siteNumber" filterable placeholder="请选择">
            <el-option v-for="item in originList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </div>
        <div style="min-width:200px;margin: 5px 10px 18px 0px;">
          <span style="font-size: 13px; padding: 0px 8px 0px 0px">所属客户</span>
          <el-select v-model="customerId" remote reserve-keyword :remote-method="PagecustomerPeople"  filterable placeholder="请选择">
            <el-option v-for="item2 in clientList" :key="item2.id" :label="item2.name" :value="item2.id">
            </el-option>
          </el-select>
        </div>
        </template>
        <div style="min-width:100px;margin: 5px 10px 18px 0px;">
          <el-button type="primary" data-tracker="P23-1" @click="Pagequeryshipping_squery()" icon="el-icon-search">查询</el-button>
          <el-button type="primary" data-tracker="P23-2" plain @click="resetquery()" icon="el-icon-refresh-left">重置</el-button>
        </div>

      </div>
    </div> -->
    <!-- 内容 -->
    <div class="sharecontent">
      <!-- 头部内容 -->
      <div class="sharecontent_header">
        <div class="sharecontent_header_right">
          <el-button v-permission="'company_consignee_create'" type="primary" plain data-tracker="P23-3"
            @click="handleClick()" style="margin:0px 10px 10px 0px">新增</el-button>
          <el-button v-permission="'company_consignee_delete'" type="primary" plain data-tracker="P23-4"
            @click="PagedeleteShipping()" style="margin:0px 10px 10px 0px">删除</el-button>
        </div>
        <div style="display: flex;">
          <div>
            <el-button type="primary" size="mini" plain @click="resetquery"
              style="margin: 0px 10px 0px 0px;">重置</el-button>
            </el-button>
          </div>
          <div>
            <SettinUp :settype="28" :objSettinUpdata="objSettinUp" key="3"></SettinUp>
          </div>
        </div>
      </div>
      <!-- 表单内容 -->
      <div class="sharecontent_content">
        <TableHeader ref="TableHeaderList" :header_data_list="header_data_list" :HeaderData="HeaderData"
          :propMap="propMap" :objheader="objheader" @childEvent="handleDelete" @orderIdNumsEvent="handleDeleteType"
          :operation_type="operation_type" @handleClickDetail_second="handleClickDetail_second"
          @handleClickEdit_second="handleClickEdit_second" particulars="finance_invoice_book_list"
          redact="finance_invoice_book_update">
        </TableHeader>
        <!-- <el-table :data="tableData" border stripe style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" fixed width="55"> </el-table-column>
          <el-table-column type="index" fixed label="序号" width="55"></el-table-column>
          <el-table-column fixed prop="name" label="收货人" width="200">
          </el-table-column>
          <el-table-column prop="mobile" label="联系方式" width="200">
          </el-table-column>
          <el-table-column prop="province" label="收货地址" width="200"> </el-table-column>
          <el-table-column prop="customer_cahe_name" label="收货公司" width="200">
          </el-table-column>
          <el-table-column prop="customer_name" label="所属客户" width="200" v-if="!isCustomer">
          </el-table-column>
          <el-table-column prop="branch_name" label="所属组织" width="280" v-if="!isCustomer">
          </el-table-column>
          <el-table-column prop="remark" label="备注"> </el-table-column>
          <el-table-column fixed="right" d="right" label="操作" width="100">
            <template slot-scope="scope">
              <el-button v-permission="'company_consignee_detail'" @click="handleClick(scope.row, '1')" data-tracker="P23-5" type="text" size="small">查看</el-button>
              <el-button v-permission="'company_consignee_update'" type="text" size="small" data-tracker="P23-6" @click="handleClick(scope.row, '2')">编辑</el-button>
            </template>
          </el-table-column>
        </el-table> -->
      </div>
      <div class="sharecontent_header_left">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="queryparam.page" :page-sizes="[10, 20, 40, 50]" :page-size="queryparam.page_size"
          layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
      <!-- 弹框内容 -->
      <div>
        <el-drawer :direction="direction" :title="title_name" :visible.sync="dialogVisible" size="40%"
          :before-close="handleClose" :wrapperClosable="false">
          <div>
            <el-form :inline="true" class="demo-form-inline" label-width="130px" :model="ruleForm" :rules="rules"
              ref="ruleForm">
              <el-form-item label="收货人" prop="consigner">
                <el-input placeholder="收货人" v-model="ruleForm.consigner" :disabled="disableDetail"></el-input>
              </el-form-item>
              <el-form-item label="联系方式" prop="phone">
                <el-input placeholder="请输入收货人手机号" v-model="ruleForm.phone" :disabled="disableDetail"></el-input>
              </el-form-item>
              <el-form-item label="所属客户" prop="customer_id">
                <el-select v-model="ruleForm.customer_id" remote reserve-keyword :remote-method="searchCustomerList"
                  filterable placeholder="请选择" :disabled="disableDetail" style="width: 165px;">
                  <el-option v-for="item2 in client" :key="item2.id" :label="item2.name" :value="item2.id">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="收货公司" prop="customer_name">
                <el-input placeholder="收货公司" v-model="ruleForm.customer_name" :disabled="disableDetail"></el-input>
              </el-form-item>
              <div style="width:100%">
                <el-form-item label="收货人地址" prop="setUPvalue">
                  <el-cascader v-model="ruleForm.setUPvalue" placeholder="选择收货地址" :options="options" :props="{
                      value: 'id',
                      label: 'name',
                      children: 'next_level_data',
                    }" :disabled="disableDetail" style="width: 165px;"></el-cascader>
                </el-form-item>
                <el-form-item label="" prop="indetile">
                  <el-input placeholder="收货人详细地址" v-model="ruleForm.indetile" style="width: 165px;"
                    :disabled="disableDetail"></el-input>
                </el-form-item>
              </div>
              <el-form-item label="身份证号">
                <el-input placeholder="身份证号" v-model="ruleForm.id_card" :disabled="disableDetail"></el-input>
              </el-form-item>
              <el-form-item label="所属组织" v-if="detailShow">
                <el-select v-model="ruleForm.branch_id" clearable placeholder="请选择" :disabled="disableDetail"
                  style="width: 165px;">
                  <el-option v-for="item in originList" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="备注">
                <el-input placeholder="备注" v-model="ruleForm.comment" :disabled="disableDetail"></el-input>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button :data-tracker="orderstate == '0'?'P23-8':'P23-10'" @click="cancledata()">取 消</el-button>
              <!-- 代表创建的确定 -->
              <el-button type="primary" data-tracker="P23-7" @click="PageAddshipping('ruleForm')"
                v-if="orderstate == '0'">确 定</el-button>
              <!-- 修改的确定 -->
              <el-button type="primary" data-tracker="P23-9" @click="PageAmendshipping('ruleForm')"
                v-if="orderstate == '2'">确 定</el-button>
            </span>
          </div>
        </el-drawer>
      </div>
    </div>
  </div>
</template>

<script>
import TableHeader from "@/page/orderform/components/TableHeader/tableheader.vue";
import SettinUp from "@/page/orderform/components/settingUp/settingUp.vue";
import {
  consigneecreate,
  consigneedetail,
  consigneeupdate,
  consigneedelete,
  queryconsignee,
  customerPeople,
} from "@/api/information";
import { PagewebsiteList, regionList } from "../common/common";
export default {
  components: { TableHeader, SettinUp },
  data() {
    return {
      title_name: "收货人",
      customerId: "",
      // 客户
      client: [],
      clientList: "",
      // 分页功能
      total: 0, //总条数
      currentPage4: 1,
      direction: "rtl",
      inputPhone: "",
      // 地址库
      options: [],
      queryvalue: [], //查询的value
      inputname: "", //收货人名字
      detailvalue: "", //详细地址
      siteNumber: "", //所属组织查询
      deletparam: {
        id: [],
      },
      queryparam: {
        // name: "", //姓名
        // province_id: 0, //省id
        // city_id: 0, //市ID
        // district_id: 0, //区ID
        // address: "", //详细地址
        // branch_id: 0, //
        page: 1,
        page_size: 20,
        // mobile: "",
        // customer_id: '',
      }, //收货方搜索接口
      ruleForm: {
        consigner: "", //收货人
        setUPvalue: [], //创建vlaue
        indetile: "", //详细地址
        phone: "", //电话
        comment: "", //备注
        branch_id: "", //所属组织
        customer_id: "", //收货公司
        id_card: "", //身份证号
        customer_name: "", //收货公司
      },
      rules: {
        consigner: [
          { required: true, message: "请填写收货人名字", trigger: "blur" },
        ],
        setUPvalue: [
          { required: true, message: "请填写收货地址", trigger: "blur" },
        ],
        phone: [
          { required: true, message: "请填写收货人手机号", trigger: "blur" },
        ],
        indetile: [
          { required: true, message: "请填写收货人详细地址", trigger: "blur" },
        ],
        customer_id: [
          { required: true, message: "请填写客户", trigger: "blur" },
        ],
      },
      tableData: [],
      orderstate: "", //新增状态0 编辑状态2
      consignerID: "", //收货人的ID
      dialogVisible: false,
      originList: [], //所属组织库
      detailShow: false,
      disableDetail: false,
      isCustomer: false, // 是否是客户
      // ==========
      header_data_list: [],
      propMap: {},
      obj_list: {},
      HeaderData: [],
      waybillIdNums: [],
      totalPage: 0,
      operation_type: 1,
    };
  },
  mounted() {
    this.provincectiy();
    this.Pagequeryshipping();
    this.siteList();
    // 客户
    this.PagecustomerPeople();
    const user = JSON.parse(localStorage.getItem("user") || "{}");
    this.isCustomer = user.type === 1;
  },
  methods: {
    // 模糊搜索
    searchCustomerList(val, id) {
      var param = {
        name: val + "",
        is_send: "1",
        is_receive: "",
        id: id == undefined ? "" : id + "",
      };
      customerPeople(param).then((res) => {
        var res = res.data;
        this.client = res;
      });
    },
    // 客户管理的人
    PagecustomerPeople(val) {
      var param = {
        name: val + "",
        is_send: "1",
        is_receive: "",
      };
      customerPeople(param).then((res) => {
        var res = res.data;
        this.clientList = res;
      });
    },
    // 组织管理页面
    siteList() {
      PagewebsiteList("3").then((res) => {
        this.originList = res.data;
      });
    },
    provincectiy() {
      regionList(0).then((res) => {
        this.options = res.data;
      });
    },
    // 分页功能
    // 一页有多少条
    handleSizeChange(val) {
      this.queryparam.page_size = val;
      //  数据列表接口
      this.Pagequeryshipping();
    },
    // 当前第几页
    handleCurrentChange(val) {
      this.queryparam.page = val;
      //  数据列表接口
      this.Pagequeryshipping();
    },
    // 处理列表的数据
    DataState(res) {
      // var nametable = [];
      // for (var i = 0; i < res.length; i++) {
      //   nametable.push({
      //     id: res[i].id,
      //     name: res[i].name,
      //     mobile: res[i].mobile,
      //     province:
      //       res[i].province_name +
      //       "/" +
      //       res[i].city_name +
      //       "/" +
      //       res[i].district_name +
      //       "/" +
      //       res[i].address,
      //     remark: res[i].remark,
      //     branch_name: res[i].branch_name,
      //     customer_cahe_name:res[i].customer_cahe_name,
      //     customer_name: res[i].customer_name,
      //   });
      // }
      // this.tableData = nametable;
    },
    // 创建发运方
    PageAddshipping(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var paramtime = {
            Name: this.ruleForm.consigner, //姓名
            Mobile: this.ruleForm.phone, //电话
            Remark: this.ruleForm.comment, //备注
            province_id: this.ruleForm.setUPvalue[0], //省id
            city_id: this.ruleForm.setUPvalue[1], //市ID
            district_id: this.ruleForm.setUPvalue[2], //区ID
            Address: this.ruleForm.indetile, //详细地址
            customer_id:
              this.ruleForm.customer_id == "" ? 0 : this.ruleForm.customer_id, //收货公司
            id_card: this.ruleForm.id_card, //身份证号
            customer_name: this.ruleForm.customer_name, //收货公司
          };
          consigneecreate(paramtime).then((res) => {
            if (res.code == 10000) {
              this.$message({
                message: "创建成功",
                type: "success",
              });
              // 关闭页面
              this.closepop();
              // 调用收货方搜索接口
              this.Pagequeryshipping();
            }
          });
        }
      });
    },
    // 发运方编辑
    PageAmendshipping(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var paramtime = {
            id: this.consignerID,
            name: this.ruleForm.consigner,
            mobile: this.ruleForm.phone,
            // branch_id: Number(this.ruleForm.branch_id),
            remark: this.ruleForm.comment,
            province_id: this.ruleForm.setUPvalue[0],
            city_id: this.ruleForm.setUPvalue[1],
            district_id: this.ruleForm.setUPvalue[2],
            address: this.ruleForm.indetile,
            customer_id:
              this.ruleForm.customer_id == "" ? 0 : this.ruleForm.customer_id,
            id_card: this.ruleForm.id_card, //身份证号
            customer_name: this.ruleForm.customer_name, //收货公司
          };
          consigneeupdate(paramtime).then((res) => {
            if (res.code == 10000) {
              this.$message({
                message: "编辑成功",
                type: "success",
              });
              // 关闭页面
              this.closepop();
              // 调用收货方搜索接口
              this.Pagequeryshipping();
            }
          });
        } else {
          // console.log("error submit!!");
          return false;
        }
      });
    },
    // 发运方详情
    PageDetailsshipping(id) {
      var param = {
        id: id,
      };
      consigneedetail(param).then((res) => {
        var res = res.data.list;
        this.ruleForm.consigner = res.name;
        this.ruleForm.phone = res.mobile;
        this.ruleForm.indetile = res.address;
        this.ruleForm.comment = res.remark;
        this.searchCustomerList("", res.customer_id);
        this.ruleForm.customer_id = res.customer_id == 0 ? "" : res.customer_id;
        this.ruleForm.id_card = res.id_card;
        (this.ruleForm.customer_name = res.customer_name), //收货公司
          (this.ruleForm.branch_id =
            res.branch_id == 0 ? "" : Number(res.branch_id));
        this.ruleForm.setUPvalue = [
          res.province_id,
          res.city_id,
          res.district_id,
        ];
      });
    },
    // 收货方删除
    PagedeleteShipping() {
      // var str = JSON.stringify(this.deletparam);
      // // console.log(typeof str);
      var param = {
        id: this.waybillIdNums,
      };
      consigneedelete(param).then((res) => {
        if (res.code == 10000) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          // 关闭页面
          this.closepop();
          // 调用收货方搜索接口
          this.Pagequeryshipping();
        }
      });
    },
    // 收货方搜索
    Pagequeryshipping() {
      // this.queryparam.name = this.inputname;
      // this.queryparam.branch_id = Number(this.siteNumber);
      // this.queryparam.mobile = this.inputPhone;
      // this.queryparam.customer_id = this.customerId.toString(),
      var param = {
        ...this.queryparam,
        ...this.obj_list,
        header_id: 28 + "",
      };
      queryconsignee(param).then((res) => {
        this.orders = res.data.list || [];
        this.orders.unshift({});
        this.header_data_list = this.orders;
        this.HeaderData = res.data.item_list;
        this.Fntransformation(this.HeaderData);
        this.total = res.data.pagination.total;
      });
    },
    // 修改和创建接口
    handleClick(row, type) {
      if (row == undefined) {
        this.title_name = "创建收货人";
        this.orderstate = "0";
        this.openpop();
        this.detailShow = false;
        this.disableDetail = false;
      }
      // 详情
      else if (row != "" && type == 1) {
        this.title_name = "查看收货人";
        // 将修改的状态赋值为空
        this.orderstate = "";
        this.PageDetailsshipping(row.id);
        this.openpop();
        this.detailShow = true;
        this.disableDetail = true;
      } else if (row != "" && type == 2) {
        this.title_name = "修改收货人";
        this.orderstate = "2";
        this.consignerID = row.id;
        this.PageDetailsshipping(row.id);
        this.openpop();
        this.detailShow = false;
        this.disableDetail = false;
      }
    },
    // 删除的全部id
    handleSelectionChange(val) {
      var deleid = [];
      for (var i = 0; i < val.length; i++) {
        deleid.push(val[i].id);
      }
      this.deletparam.id = deleid;
    },
    // 关闭弹框的函数
    closepop() {
      this.dialogVisible = false;
    },
    // 打开弹框
    openpop() {
      this.dialogVisible = true;
    },
    // 点击取消
    cancledata() {
      // 清空添加的数据
      this.clearList();
      // 关闭页面
      this.closepop();
    },
    // 点击关闭
    handleClose() {
      // 清空添加的数据
      this.clearList();
      // 关闭页面
      this.closepop();
    },
    // 清空回显的数据
    clearList() {
      Object.assign(this.$data.ruleForm, this.$options.data().ruleForm);
    },
    Pagequeryshipping_squery() {
      this.queryparam.page_size = 20;
      this.queryparam.page = 0;
      this.Pagequeryshipping();
    },
    // 重置数据内容
    resetquery() {
      // 查询的数据清除
      // this.queryparam.page_size = 10
      // this.queryparam.page = 0
      // this.siteNumber = "";
      // this.queryvalue = [];
      // this.inputname = "";
      // this.detailvalue = "";
      // this.inputPhone = "";
      // this.customerId = "";
      // this.Pagequeryshipping();
      this.$refs.TableHeaderList.childMethod();
      this.queryparam.page = 1;
    }, // ===
    objheader(obj) {
      var data = {
        header_id: 28 + "",
        ...obj,
        ...this.queryparam,
      };
      this.obj_list = obj;
      queryconsignee(data).then((res) => {
        this.header_data_list = res.data.list;
        if (res.code === 10000 && res.data.list !== null) {
          this.header_data_list.unshift({});
          this.total = res.data.pagination.total;
        } else {
          this.header_data_list = [{}];
          this.total = 0;
          this.orderslength = 0;
        }
      });
    },
    Fntransformation(obj) {
      const result = {};
      for (const prop in obj) {
        if (obj.hasOwnProperty(prop)) {
          if (obj[prop].send_name !== "") {
            result[obj[prop].name] = obj[prop].show_name;
          } else {
            result[obj[prop].name] = obj[prop].name;
          }
        }
      }
      this.propMap = result;
    },
    handleDelete(index, id, type) {
      this.handleJumpDetail(id);
    },
    handleDeleteType(value) {
      this.waybillIdNums = value;
    },
    handleClickDetail_second(val) {
      this.handleClick(val, "1");
    },
    handleClickEdit_second(val) {
      this.handleClick(val, "2");
    },
    objSettinUp() {
      this.Pagequeryshipping();
    },
  },
};
</script>

<style lang="less" scoped>
.shareContainer {
  padding: 10px 15px;
  background-color: #fff;

  .shareheader {
    margin: 0 auto;

    .shareheader_top {
      display: flex;
      flex-wrap: wrap;
    }
  }

  .sharecontent_header_left {
    display: flex;
    justify-content: flex-end;
    margin: 10px 0px 0px 0px;
  }

  // 内容
  .sharecontent {
    margin: 0 auto;
    background: white;

    // 头部内容
    .sharecontent_header {
      display: flex;
      justify-content: space-between;
      /* .sharecontent_header_right {
          display: inline-block;
        } */
    }

    // 表单内容
    .sharecontent_content {
    }
  }
}

.dialog-footer {
  position: fixed;
  width: 40%;
  text-align: center;
  bottom: 20px;
}
</style>